package zj.com.fragments;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;
import org.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.List;

import zj.com.activities.CaptureActivity;
import zj.com.activities.ChoiceJcxActivity;
import zj.com.activities.DqrEditActivity;
import zj.com.activities.JcxDetailActivity;
import zj.com.adapter.DqrAdapter;
import zj.com.adapter.JcxAdapter;
import zj.com.app.ZjApplication;
import zj.com.db.DBUtil;
import zj.com.model.Dic;
import zj.com.model.InspectContent;
import zj.com.model.InspectItem;
import zj.com.model.JcdInfoDB;
import zj.com.model.JcxDB;
import zj.com.model.JcxDetailDB;
import zj.com.model.PgScan;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.util.DialogUtil;
import zj.com.util.LogUtils;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/4.
 * 检查项
 */
public class DqrLeftFragment extends Fragment implements View.OnClickListener {



    public  ProgressDialog dialog;
    public List<JcdInfoDB> list;
    public RelativeLayout error_rl;
    public ListView listView;
    public DqrAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dqr, container, false);
        initView(view);
        return view;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }



    public void initView(View view){

        list = new ArrayList<>();
        error_rl = (RelativeLayout) view.findViewById(R.id.error_rl);
        listView = (ListView) view.findViewById(R.id.listView);
        adapter = new DqrAdapter(list,getActivity());
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), DqrEditActivity.class);
                ZjApplication.isAcc = -1;
                ZjApplication.dqrJcdInfo = list.get(position);
                startActivity(intent);
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtils.getInstance().error("=========leftFramgent 数据刷新=============");
        obtainData();

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if(getUserVisibleHint()){
            LogUtils.getInstance().error("=========数据刷新=============");
        }

        LogUtils.getInstance().error("=========数据刷新2=============");

    }



    public void obtainData(){
        dialog = DialogUtil.createProgressDialog(getActivity(), "提示", "获取数据中...");
        NetManager.getInstance(getActivity()).obtainGetToBeConfirmedInspectionList(new OnResponseListener<List<JcdInfoDB>>() {
            @Override
            public void onComplete(boolean isOK, List<JcdInfoDB> result, int errorCode, String errorMsg) {

                dialog.dismiss();
                if (isOK) {

                    list = result;
                    if (list != null && list.size() > 0) {
                        error_rl.setVisibility(View.GONE);
                    } else {
                        error_rl.setVisibility(View.VISIBLE);

                    }
                    adapter.setDataChange(result);
                } else {
                    error_rl.setVisibility(View.VISIBLE);

                    Toast.makeText(getActivity(), "获取失败", Toast.LENGTH_SHORT);
                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                error_rl.setVisibility(View.VISIBLE);
                dialog.dismiss();

                Toast.makeText(getActivity(), "获取失败", Toast.LENGTH_SHORT);

            }
        });
    }

    @Override
    public void onClick(View v) {


    }

    public int totalCount = 0;
    public int count = 0;


}
